package com.odianyun.springboottest.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.odianyun.springboottest.model.po.Article;
import com.odianyun.springboottest.repository.ArticleRepository;
import com.odianyun.springboottest.service.ArticleService;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

/**
 * @description:
 * @author: EDZ
 * @time: 20:01
 * @date: 2021/7/19
 */
@Service
public class ArticleServiceImpl implements ArticleService {

    @Resource
    private MongoTemplate mongoTemplate;

    @Resource
    private ArticleRepository  articleRepository;

    /**
     * id存在则更新
     * @param article
     * @return
     */
    @Override
    public String save(Article article) {
//        mongoTemplate.save(article);
        articleRepository.save(article);
        return article.getId();
    }

    @Override
    public String insert(Article article) {
        mongoTemplate.insert(article);
        return article.getId();
    }

    @Override
    public void batchSave(List<Article> articleList) {
        Collection<Article> result = mongoTemplate.insertAll(articleList);
        System.out.println(JSONObject.toJSONString(result));
    }

}
